<template>
  <el-container class="top">
    <el-header>
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="型号搜索：">
          <el-input v-model="value" placeholder="请输入" size="medium" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit"><i class="el-icon-search" style="margin-right:5px" />查询</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main>
      <el-row>
        <el-button type="warning" @click="open('add')">
          <i class="el-icon-circle-plus-outline" style="margin-right:5px" />新建
        </el-button>
      </el-row>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{ backgroundColor: '#f3f6fb', fontweight: 400 }"
      >
        <el-table-column
          label="序号"
          width="100px"
        >
          <template slot-scope="scope">
            <span>{{ scope.$index+(queryObj.pageIndex - 1) * queryObj.pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="型号名称"
        />
        <el-table-column
          prop="model"
          label="型号编码"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备图片"
        >
          <template #default="{row}">
            <el-avatar :src="row.image" />
          </template>
        </el-table-column>
        <el-table-column
          prop="vmCol"
          label="货道行"
        />
        <el-table-column
          prop="vmRow"
          label="货道列"
        />
        <el-table-column
          prop="channelMaxCapacity"
          label="设备容量"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          show-overflow-tooltip
        >
          <template #default="{row}">
            <el-button type="text" @click="open('edit',row)">修改</el-button>
            <el-button type="text" style="color:red" @click="del(row.typeId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-dialog
      :title="form.typeId?'修改设备类型':'新增设备类型'"
      :visible.sync="dialogVisible"
      width="30%"
      @close="close"
    >
      <el-form
        ref="form"
        :label-position="labelPosition"
        label-width="120px"
        :model="form"
        :rules="rules"
      >
        <el-form-item label="型号名称:" prop="name">
          <el-input v-model="form.name" maxlength="10" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item label="型号编码:" prop="model">
          <el-input v-model="form.model" maxlength="15" show-word-limit placeholder="请输入（限制数字、字母、中划线、下划线）" />
        </el-form-item>
        <el-form-item label="货道行数:" prop="vmRow">
          <el-input-number
            v-model="form.vmRow"
            controls-position="right"
            :min="0"
            :max="10"
            style="width:100%"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="货道列数:" prop="vmCol">
          <el-input-number
            v-model="form.vmCol"
            controls-position="right"
            :min="0"
            :max="10"
            style="width:100%"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="货道容量:" prop="channelMaxCapacity">
          <el-input-number
            v-model="form.channelMaxCapacity"
            controls-position="right"
            :min="0"
            :max="10"
            style="width:100%"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="设备图片:">
          <el-upload
            class="avatar-uploader"
            action=""
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <p>支持扩展名：jpg、png，文件不得大于100kb</p>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>

<script>
import { getList, addList, editList, delList } from '@/api/devicetype'
export default {
  name: 'Devicetype',
  data() {
    return {
      dialogVisible: false,
      value: '',
      tableData: [],
      queryObj: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      type: 'add',
      labelPosition: 'right',
      form: {
        vmRow: null,
        vmCol: null,
        name: '',
        channelMaxCapacity: null,
        model: '',
        image: '',
        typeId: ''
      },
      rules: {
        name: [{ required: true, message: '请输入型号名称', trigger: 'blur' }],
        model: [{ required: true, message: '请输入型号编码', trigger: 'blur' }],
        vmCol: [{ required: true, message: '请输入货道行数', trigger: 'blur' }],
        vmRow: [{ required: true, message: '请输入货道列数', trigger: 'blur' }],
        channelMaxCapacity: [{ required: true, message: '请输入货道容量', trigger: 'blur' }]
      },
      imageUrl: ''

    }
  },
  created() {
    this.getManagementList()
  },
  methods: {
    async getManagementList() {
      const data = await getList(this.queryObj)
      this.tableData = data.currentPageRecords
    },
    onSubmit() {
      this.queryObj.name = this.value
      this.getManagementList()
    },
    async del(id) {
      try {
        await delList(id)
        this.$message.success('删除成功')
        this.getManagementList()
      } catch (error) {
        this.$message.error('该售货机类型在使用')
      }
    },
    open(type, row) {
      this.type = type
      if (type === 'edit') {
        this.form.typeId = row.typeId
        this.form.name = row.name
        this.form.model = row.model
        this.form.vmCol = row.vmCol
        this.form.vmRow = row.vmRow
        this.form.image = row.image
        this.form.channelMaxCapacity = row.channelMaxCapacity
      }
      this.dialogVisible = true
    },
    async submit() {
      this.$refs.form.validate()
      try {
        if (this.type === 'add') {
          await addList(this.form)
          this.$message.success('新增成功')
          this.getManagementList()
        }
        if (this.type === 'edit') {
          await editList(this.form)
          this.$message.success('修改成功')
          this.getManagementList()
        }
        this.close()
      } catch (error) {
        this.$message.error('该售货机类型在使用')
      }
    },
    close() {
      this.$refs.form.resetFields()
      this.form = {
        vmRow: null,
        vmCol: null,
        name: '',
        channelMaxCapacity: null,
        model: '',
        image: '',
        typeId: ''
      }
      this.dialogVisible = false
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style  lang='scss'>
.top{
  margin-top: 70px;
}
.el-row {
  margin-bottom: 10px;
}
.btn {
  background-color: #fbf4f0;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #333;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  p {
    margin-left: 120px;
    }
</style>
